<?php
	//Requiere ConfigManager.php
	require_once "config_manager.php";
	
	class DAO
	{
		private static $config;
		
		private static function connect_to_db()
		{
			$config = ConfigManager::getInstance();
			$server = $config->get_mysql_server();
			$user = $config->get_mysql_user();
			$password = $config->get_mysql_password();
			$catalog = $config->get_mysql_catalog();
			
			$connection = mysql_connect($server, $user, $password);
			
			if(!$connection)
			{
				throw new Exception('Could not connect to DB.');
			}
			
			mysql_select_db($catalog, $connection);
			
			return $connection;
		}
		
		public static function connect_to_db_i()
		{
			$config = ConfigManager::getInstance();
			$server = $config->get_mysql_server();
			$user = $config->get_mysql_user();
			$password = $config->get_mysql_password();
			$catalog = $config->get_mysql_catalog();
			
			$connection = mysqli_connect($server, $user, $password, $catalog);
			
			if(!$connection)
			{
				throw new Exception('Could not connect to DB.');
			}
			
			//mysqli_select_db($connection, $catalog);
			
			return $connection;
		}
		
		private static function close_connection($connection)
		{
			mysql_close($connection);
		}
		
		private static function close_connection_i($connection)
		{
			mysqli_close($connection);
		}
		
		public static function run_query($strQuery)
		{
			try
			{
				$connection = self::connect_to_db();
				
				$result = mysql_query($strQuery);
			
				self::close_connection($connection);
				
				return $result;
			} catch(Exception $e)
			{
				echo 'Caught exception: ', $e->getMessage(), "\n";
			}
		}
		
		public static function run_proc($strProc, $strParams)
		{
			try
			{
				$connection = self::connect_to_db_i();
				
				mysqli_query($connection, "CALL $strProc($strParams)");
				mysqli_query($connection, "SELECT $strParams");
				
				$result = mysqli_store_result($connection);
				
				self::close_connection_i($connection);
				
				return $result;
			} catch(Exception $e)
			{
				echo 'Caught exception: ', $e->getMessage(), "\n";
			}
		}
		
		public static function run_non_query($strQuery)
		{
			$return_value = -1;
			
			try
			{
				$connection = self::connect_to_db();
				
				mysql_query($strQuery);
				$return_value = mysql_insert_id($connection);
				
				self::close_connection($connection);
				
			} catch (Exception $e)
			{
				echo 'Caught exception: ', $e->getMessage(), "\n";
			}
			
			return $return_value;
		}
		
		function fnSQLtoHTML($sSQL) 
		{
			$sRetVal = "<table border=1>\n";
			$sRetVal .= "<tr><th colspan=" . mysql_num_fields($result) . ">";
			$sRetVal .= mysql_field_table($result,0) . "</th></tr>";
			$sRetVal .= "<tr>";
			$i=0;
			
			while ($i < mysql_num_fields($result)) {
			$sRetVal .= "<th>" . mysql_field_name($result, $i) . "</th>";
			$i++;
			}
			
			$sRetVal .= "</tr>";
			
			while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$sRetVal .= "\t<tr>\n";
				
				foreach ($line as $col_value) {
					$sRetVal .= "\t\t<td>$col_value</td>\n";
				}
			
				$sRetVal .= "\t</tr>\n";
			}
			
			$sRetVal .= "</table>\n";
			
			mysql_free_result($result);
			
			return ($sRetVal);
		}
	}
?>